package dsx.mongo;

import java.util.Date;

import org.bson.Document;

import com.mongodb.BasicDBObject;

public class DataFetcher {

	public static String getString(Object doc, String key) {
		try {
			if (doc.getClass().getName().equals("com.mongodb.BasicDBObject"))
				return ((BasicDBObject) doc).getString(key);
			else if (doc.getClass().getName().equals("org.bson.Document"))
				return ((Document) doc).getString(key);
			else
				return null;
		} catch (Exception ex) {
			return null;
		}
	}

	public static String getString(Object doc, String key, String defaultValue) {
		try {
			String value = null;
			if (doc.getClass().getName().equals("com.mongodb.BasicDBObject"))
				value = ((BasicDBObject) doc).getString(key);
			else if (doc.getClass().getName().equals("org.bson.Document"))
				value = ((Document) doc).getString(key);
			else
				value = defaultValue;
			if (value == null)
				return defaultValue;
			else 
				return value;
		} catch (Exception ex) {
			return defaultValue;
		}
	}

	public static String getString(Document doc, String key) {
		try {
			return doc.getString(key);
		} catch (Exception ex) {
			return null;
		}
	}

	public static String getString(BasicDBObject doc, String key) {
		try {
			return doc.getString(key);
		} catch (Exception ex) {
			return null;
		}
	}

	public static String getString(BasicDBObject doc, String key,
			String defaultValue) {
		try {
			String value = doc.getString(key);
			if (value == null)
				return defaultValue;
			return value;
		} catch (Exception ex) {
			return defaultValue;
		}
	}

	public static Date getDate(Object doc, String key) {
		try {
			if (doc.getClass().getName().equals("com.mongodb.BasicDBObject"))
				return ((BasicDBObject) doc).getDate(key);
			else if (doc.getClass().getName().equals("org.bson.Document"))
				return ((Document) doc).getDate(key);
			else
				return null;
		} catch (Exception ex) {
			return null;
		}
	}

	public static Date getDate(Document doc, String key) {
		try {
			return doc.getDate(key);
		} catch (Exception ex) {
			return null;
		}
	}

	public static Date getDate(BasicDBObject doc, String key) {
		try {
			return doc.getDate(key);
		} catch (Exception ex) {
			return null;
		}
	}

	public static Date getDate(BasicDBObject doc, String key, Date defaultValue) {
		try {
			Date value = doc.getDate(key);
			if (value == null)
				return defaultValue;
			else
				return value;
		} catch (Exception ex) {
			return null;
		}
	}

	public static int getInt(Object doc, String key) {
		try {
			if (doc.getClass().getName().equals("com.mongodb.BasicDBObject"))
				return ((BasicDBObject) doc).getInt(key);
			else if (doc.getClass().getName().equals("org.bson.Document"))
				return ((Document) doc).getInteger(key, -1);
			else
				return -1;
		} catch (Exception ex) {
			try {

				if (doc.getClass().getName()
						.equals("com.mongodb.BasicDBObject"))
					return Integer.parseInt(((BasicDBObject) doc)
							.getString(key));
				else if (doc.getClass().getName().equals("org.bson.Document"))
					return Integer.parseInt(((Document) doc).getString(key));
				else
					return -1;
			} catch (Exception ex1) {
				return -1;
			}
		}
	}

	public static int getInt(Document doc, String key) {
		try {
			return doc.getInteger(key, -1);
		} catch (Exception ex) {
			try {
				return Integer.parseInt(doc.getString(key));
			} catch (Exception ex1) {
				return -1;
			}
		}
	}

	public static int getInt(BasicDBObject doc, String key) {
		try {
			return doc.getInt(key);
		} catch (Exception ex) {
			try {
				return Integer.parseInt(doc.getString(key));
			} catch (Exception ex1) {
				return -1;
			}
		}
	}

	public static int getInt(BasicDBObject doc, String key, int defaultValue) {
		try {
			return doc.getInt(key);
		} catch (Exception ex) {
			try {
				String strValue = doc.getString(key);
				if (strValue == null || strValue.isEmpty())
					return defaultValue;
				return Integer.parseInt(strValue);
			} catch (Exception ex1) {
				return -1;
			}
		}
	}

	public static double getDouble(Object doc, String key) {
		try {
			if (doc.getClass().getName().equals("com.mongodb.BasicDBObject"))
				return ((BasicDBObject) doc).getDouble(key);
			else if (doc.getClass().getName().equals("org.bson.Document"))
				return ((Document) doc).getDouble(key);
			else
				return 0;
		} catch (Exception ex) {
			try {
				if (doc.getClass().getName()
						.equals("com.mongodb.BasicDBObject"))
					return Double.parseDouble(((BasicDBObject) doc)
							.getString(key));
				else if (doc.getClass().getName().equals("org.bson.Document"))
					return Double.parseDouble(((Document) doc).getString(key));
				else
					return 0;
			} catch (Exception ex1) {
				return 0;
			}
		}
	}

	public static double getDouble(Document doc, String key) {
		try {
			return doc.getDouble(key);
		} catch (Exception ex) {
			try {
				return Double.parseDouble(doc.getString(key));
			} catch (Exception ex1) {
				return 0;
			}
		}
	}

	public static double getDouble(BasicDBObject doc, String key) {
		try {
			return doc.getDouble(key);
		} catch (Exception ex) {
			try {
				return Double.parseDouble(doc.getString(key));
			} catch (Exception ex1) {
				return 0;
			}
		}
	}

	public static double getDouble(BasicDBObject doc, String key,
			double defaultValue) {
		try {
			return doc.getDouble(key);
		} catch (Exception ex) {
			try {
				String strValue = doc.getString(key);
				if (strValue == null || strValue.isEmpty())
					return defaultValue;
				return Double.parseDouble(strValue);
			} catch (Exception ex1) {
				return -1;
			}
		}
	}

	public static Object getObject(Object doc, String key) {
		try {
			if (doc.getClass().getName().equals("com.mongodb.BasicDBObject"))
				return ((BasicDBObject) doc).get(key);
			else if (doc.getClass().getName().equals("org.bson.Document"))
				return ((Document) doc).get(key);
			else
				return null;
		} catch (Exception ex) {
			return null;
		}
	}

	public static Object getObject(Document doc, String key) {
		try {
			return doc.get(key);
		} catch (Exception ex) {
			return null;
		}
	}

	public static Object getObject(BasicDBObject doc, String key) {
		try {
			return doc.get(key);
		} catch (Exception ex) {
			return null;
		}
	}

	public static long getLong(BasicDBObject doc, String key, int defaultValue) {
		try {
			return doc.getLong(key);
		} catch (Exception ex) {
			try {
				String strValue = doc.getString(key);
				if (strValue == null || strValue.isEmpty())
					return defaultValue;
				return Long.parseLong(strValue);
			} catch (Exception ex1) {
				return -1;
			}
		}
	}
}
